package com.education.module.examdate;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.education.domain.Examdate;
import com.education.framework.base.BaseServices;
import com.education.framework.dao.IDao;
import com.education.framework.domain.SearchParams;
import com.education.framework.page.Page;

@Service
public class ExamdateServices extends BaseServices implements IDao<Examdate>{

	@Override
	public List<Examdate> find(SearchParams searchParams, Page page) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,code,startDate,endDate,description FROM examDate");
		String lp = " where ";
		List<Object> argsList = new ArrayList<Object>();
		if(null != searchParams){
			if(null != searchParams.get("title") && !"".equals((String)searchParams.get("title"))){
				sql.append(lp).append(" title like ? ");
				argsList.add("%" + searchParams.get("title") + "%");
				lp = " and ";
			}
		}
		Object[] args = argsList.toArray();
		List<Examdate> list = dao.query(pageSQL(sql.toString(),page),args,new ExamdateRowmapper());
		page.setTotalItem(findRecordCount("select count(id) as cNum from examDate ",new Object[]{}));
		return list;
	}
	
	public List<Examdate> find() {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,code,startDate,endDate,description FROM examDate");
		
		List<Examdate> list = dao.query(sql.toString(),new ExamdateRowmapper());
		return list;
	}

	@Override
	public int save(Examdate obj) {
		 
 StringBuffer sql = new StringBuffer(); 
 sql.append("insert into examDate ( "); 
 sql.append("id,code,startDate,endDate,description ");  
 sql.append(" ) values(?,?,?,?,?) "); 
 Object[] args = {obj.getId(),obj.getCode(),obj.getStartdate(),obj.getEnddate(),obj.getDescription() };
		 
		 dao.update(sql.toString(), args);
		return dao.queryForInt("SELECT LAST_INSERT_ID()"); 
	}

	@Override
	public Examdate findForObject(Integer id) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,code,startDate,endDate,description FROM examDate ");
		sql.append(" where id=? ");
		
		Object[] args = {id};
		return dao.queryForObject(sql.toString(),args,new ExamdateRowmapper());
	}

	@Override
	public void update(Examdate obj) {
		 
 StringBuffer sql = new StringBuffer(); 
 sql.append("update examDate "); 
 sql.append("set  "); 
 sql.append("code=?,startDate=?,endDate=?,description=? where id=?  "); 
 Object[] args = {obj.getCode(),obj.getStartdate(),obj.getEnddate(),obj.getDescription(),obj.getId() };
		 
		 dao.update(sql.toString(), args);
	}

	@Override
	public void delete(Integer id) {
		String sql = "delete from examDate where id=?";
		dao.update(sql, new Object[]{id});
	}

	@Override
	public boolean findIsExist(String name) {
		return false;
	}
	
	private class ExamdateRowmapper implements RowMapper<Examdate> {
		@Override
		public Examdate mapRow(ResultSet rs, int arg1) throws SQLException {
			Examdate obj = new Examdate();
			obj.setId(rs.getInt("id")); 
obj.setStartdate(rs.getString("startDate")); 
obj.setDescription(rs.getString("description")); 
obj.setEnddate(rs.getString("endDate")); 
obj.setCode(rs.getString("code")); 

			return obj;
		}
	}

}
